<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;
use think\Cache;

class SaleAttrdb extends Model
{
    use SoftDelete;

    public $param = array();

    protected $name = 'sale_attr';

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr['partner_id'] = PARTNER_ID;
        if (! isset($new_arr['site_id'])) {
            $new_arr['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }

    protected $type = [
        'create_time' => 'timestamp:Y-m-d',
        'delete_time' => 'timestamp:Y-m-d'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    protected $insert = [
        'partner_id' => PARTNER_ID,
        'sale_attr_sign',
        'goods_sku'
    ];
    // 自动完成唯一标识
    protected function setSaleAttrSignAttr($value)
    {
        if ($value) {
            return $value;
        }
        return md5(time() . round(1000, 9999));
    }
    // 非自动完成销售价格
    protected function isSalePriceAttr($value)
    {
        if (is_numeric($value)) {
            return $value;
        }
        if ($value == 'on') {
            return 1;
        } else {
            return 2;
        }
    }

    public function scopeGoodsSku($query, $goods_sku)
    {
        $query->where('goods_sku', 'IN', $goods_sku);
    }
    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        $query->where('partner_id', PARTNER_ID);
        if (isset($this->param['site_id'])) {
            if ($this->param['site_id'] == 0 || $this->param['site_id'] == '') {
                $query->where('site_id', 'null');
            } else {
                $query->where('site_id', $this->param['site_id']);
            }
        } else {
            if (! empty(SITE_ID)) {
                $query->where('site_id', SITE_ID);
            } else {
                $query->where('site_id', 'null');
            }
        }
        if (isset($this->param['goods_sku']) && $this->param['goods_sku']) {
            $query->goodsSku($this->param['goods_sku']);
        }
    }
    // 复制
    public function copy()
    {
        $data = $this->cache($this->cache_name('sale_attr_copy'), 86400, 'sale_attr')
            ->select();
        if (!$data->isEmpty()) {
            $data = $data->toArray();
            $add_data = array();
            if ($this->param['select_site_id'] == '') {
                $this->param['site_id'] = SITE_ID;
            } else {
                $this->param['site_id'] = $this->param['select_site_id'];
            }
            foreach ($data as $key => $val) {
                unset($val['create_time']);
                $val['site_id'] = $this->param['site_id'];
                $add_data[$key] = $val;
            }
            $this->ajaxDelete();
            $this->isUpdate(false)->saveAll($add_data);
        }
    }
    
    // 采集
    public function collect()
    {
        $data = $this->cache($this->cache_name('sale_attr_copy'), 86400, 'sale_attr')
        ->select();
        if (!$data->isEmpty()) {
            $data = $data->toArray();
            $add_data = array();
            foreach ($data as $key => $val) {
                unset($val['create_time']);
                unset($val['site_id']);
                $add_data[$key] = $val;
            }
            $this->isUpdate(false)->saveAll($add_data);
        }
    }
    // 获取列表
    public function ajaxSelect($goods_sku)
    {
        $data = $this->where('goods_sku', $goods_sku)
            ->field('sale_attr_name,sale_attr_value,is_sale_price,sale_attr_sign')
            ->cache($this->cache_name('sale_attr_select'), 86400, 'sale_attr')
            ->select();
        return $data;
    }
    // 保存数据
    public function ajaxSave()
    {
        $this->ajaxDelete();
        if (isset($this->param['sale_attr_name']) && isset($this->param['sale_attr_value']) && isset($this->param['is_sale_price'])) {
            // 有数据 批量添加 这里验证数组长度是否一直
            if (count($this->param['sale_attr_name']) != count($this->param['sale_attr_value'])) {
                return '数据错误，属性值和属性名对应不上，请联系管理员';
            }
            
            $data = array();
            for ($i = 0; $i < count($this->param['sale_attr_name']); $i ++) {
                $data[$i]['sale_attr_name'] = $this->param['sale_attr_name'][$i];
                $data[$i]['sale_attr_value'] = $this->param['sale_attr_value'][$i];
                $data[$i]['is_sale_price'] = $this->isSalePriceAttr($this->param['is_sale_price'][$i]);
                $data[$i]['goods_sku'] = $this->param['goods_sku'];
                $data[$i]['site_id'] = $this->param['site_id'];
            }
            $this->allowField(true)->saveAll($data);
        }
        Cache::clear('sale_attr');
        return 'success';
    }
    // 直接删除
    public function ajaxDelete()
    {
        if (isset($this->param['site_id'])) {
            $this->where('site_id', $this->param['site_id'])->delete();
        } else {
            $this->where('goods_sku', $this->param['goods_sku'])->delete();
        }
        Cache::clear('sale_attr');
    }
}